package com.hulk.sequence.builder;


import com.hulk.sequence.worker.Sequence;
import com.hulk.sequence.worker.impl.SnowflakeSequence2;

/**
 * 基于雪花算法，序列号生成器构建者
 * @author hulk on 2018/5/30.
 */
public class SnowflakeSeqBuilder implements SeqBuilder {

	/**
	 * 数据中心ID，值的范围在[0,31]之间，一般可以设置机房的IDC[必选]
	 */
	private Long datacenterId = null;
	/**
	 * 工作机器ID，值的范围在[0,31]之间，一般可以设置机器编号[必选]
	 */
	private Long workerId= null;

	public static SnowflakeSeqBuilder create() {
		SnowflakeSeqBuilder builder = new SnowflakeSeqBuilder();
		return builder;
	}

	@Override
	public Sequence build() {
		SnowflakeSequence2 sequence = null;
		if(this.workerId==null||this.datacenterId==null) {
			 sequence = new SnowflakeSequence2();
		} else {
			sequence = new SnowflakeSequence2(this.workerId, this.datacenterId);
		}
		return sequence;
	}

	public SnowflakeSeqBuilder datacenterId(Long datacenterId) {
		this.datacenterId = datacenterId;
		return this;
	}

	public SnowflakeSeqBuilder workerId(Long workerId) {
		this.workerId = workerId;
		return this;
	}

}
